#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.8.0
 Author:         James Chen

 Script Function:
	Makes trades for me on Castle Empire
	Note: Works only on Chrome and only on the main screen of multimonitor setup
			or if the Chrome Window is moved and only on 1920xXXXX resolution.

#ce ----------------------------------------------------------------------------

#region ---Au3Recorder generated code Start ---
Opt("WinWaitDelay",100)
Opt("WinDetectHiddenText",1)
Opt("MouseCoordMode",0)

$WinSize = WinGetPos("Castle Empire - Zeus [Welt 1] - Google Chrome")

$LeftJustified = 0
$TopJustified = 0

$RightJustified = $WinSize[2] 			;Couldnt use WinGetPos number because it thinks Chrome frame is 1936 pixels wide
$BottomJustified = $WinSize[3] 	;1176 for Desktop  this number is still weird because its bigger than the Chrome frame but we will use it still

$CenterPoint0 = $RightJustified / 2		;960 D
$CenterPoint1 = $BottomJustified / 2	;588 D

$ClickSpeed = 1

$SleepTime = 599000

$TimeoutTime = 1

; Static Variables for Type selection
$OneOne0 = $CenterPoint0 - 175		;785
$OneOne1 = $CenterPoint1 - 12 		;567

$Delta0 = 79
$Delta1 = 38

$HW0 = 1
$HW1 = 1

$HWP0= 2
$HWP1 = 1

$COIN0 = 3
$COIN1 = 2

$Horse0 = 2
$Horse1 = 3

$Bow0 = 1
$Bow1 = 3
;-----------------------------------------------------------------------------------------------

; Button Statics
$AddOffer0 = $LeftJustified + 290
$AddOffer1 = $BottomJustified - 188 	;984

$DeliverSelect0 = $CenterPoint0 - 166 	;795
$DeliverSelect1 = $CenterPoint1 + 147 	;735

$ReceiveSelect0 = $CenterPoint0 + 172 	;1136
$ReceiveSelect1 = $DeliverSelect1

$Trade0 = $CenterPoint0					;966
$Trade1 = $CenterPoint1	+ 78			;658

$TradeTab0 = $LeftJustified + 132		;122
$TradeTab1 = $BottomJustified - 140		;1030

$OK0 = $CenterPoint0 + 110				;1069
$OK1 = $CenterPoint1 + 210				;798

$BasicTab0 = $CenterPoint0 - 163		;797
$BasicTab1 = $CenterPoint1 + 133		;714

$InvTabWidth = 110

$IntermediateTab0 = $BasicTab0 + 1 * $InvTabWidth 	;911
$IntermediateTab1 = $BasicTab1

$AdvancedTab0 = $BasicTab0 + 2 * $InvTabWidth
$AdvancedTab1 = $BasicTab1

$ExpertTab0 = $BasicTab0 + 3 * $InvTabWidth
$ExpertTab1 = $BasicTab1

$InboxClose0 = $CenterPoint0 + 272		;1235
$InboxClose1 = $CenterPoint1 - 141		;445

$TraderClose0 = $CenterPoint0 + 325		;1287
$TraderClose1 = $CenterPoint1 - 185		;400

$GuildClose0 = $CenterPoint0 + 272		;1236
$GuildClose1 = $CenterPoint1 - 209		;377

$MoveFromPos0 = $LeftJustified + 195
$MoveFromPos1 = $TopJustified + 160

$MoveToPos0 = $RightJustified - 508		;1428
$MoveToPos1 = $BottomJustified - 393	;953

$TipOfTheDayOK0 = $CenterPoint0
$TipOfTheDayOK1 = $CenterPoint1 + 201	;783
;----------------------------------------------------------------------------------------------

; Number Statics
$One0 = $CenterPoint0 - 133				;835
$One1 = $CenterPoint1 + 217				;805 ;798 before

$Five0 = $One0 + 13						;848
$Five1 = $One1

$Ten0 = $Five0 + 2						;850
$Ten1 = $One1

$TwentyFour0 = $Ten0 + 5				;855
$TwentyFour1 = $One1

$TwentySeven0 = $TwentyFour0 + 1 		;856
$TwentySeven1 = $One1

$TwoHundred0 = $TwentySeven0 + 63		;919
$TwoHundred1 = $One1

$FourHundred0 = $TwoHundred0 + 82		;1001
$FourHundred1 = $One1
;------------------------------------------------------------------------------------------------

#cs
Type1	5 coin for 400 HW
Type2	400 HWP for 27coin
Type3	10 coin for 400 HWP
Type4	400 HWP for 400 Horse
Type5	200 HWP for 400 Bows
#ce

Enum $Type1 = 0, $Type2, $Type3, $Type4, $Type5, $TypeMax

HotKeySet("^{ENTER}", "end")
HotKeySet("^{`}", "SetRandom")
HotKeySet("^{1}", "SetType1")
HotKeySet("^{2}", "SetType2")
HotKeySet("^{3}", "SetType3")
HotKeySet("^{4}", "SetType4")
HotKeySet("^{5}", "SetType5")

;HotKeySet("^{7}", "ColorSearch")
HotKeySet("^{8}", "NightStockup")
HotKeySet("^{9}", "Here")
HotKeySet("^{0}", "NotHere")

$random = 1;
$MousePosX = MouseGetPos(0)
$MousePosY = MouseGetPos(1)

$WindowHandle = "TEMP"

$GetRandom = True

$Stockup = False

$DragCounter = 0

$Here = False

Sleep(2000)

Beep()

While True

If $GetRandom Then

$random = Random(0, 100, 1)

If $Stockup == False Then

Select
      Case $random >= 0 AND $random <= 70
          $random = $Type2

      Case $random > 70 AND $random <= 87
          $random = $Type3

	  Case $random > 87 AND $random <= 97
          $random = $Type1

      Case $random > 97 AND $random <= 99
          $random = $Type4

	Case $random > 99 AND $random <= 100
          $random = $Type5

EndSelect

Else

	Select

      Case $random > 0 AND $random <= 75
          $random = $Type3

	  Case $random > 75 AND $random <= 100
          $random = $Type1

	EndSelect

EndIf


EndIf

$WindowHandle = WinGetHandle("[ACTIVE]")

If Not WinActive("Castle Empire - Zeus [Welt 1] - Google Chrome","") Then WinActivate("Castle Empire - Zeus [Welt 1] - Google Chrome","")
;WinWaitActive("Castle Empire - Zeus [Welt 1] - Google Chrome","")

$DragCounter = 0;

Do
MouseMove($MoveFromPos0, $MoveFromPos1, $ClickSpeed)
MouseDown("left")
MouseMove($MoveToPos0, $MoveToPos1, $ClickSpeed)
MouseUp("left")
$DragCounter+=1
Until $DragCounter == 3

MouseWheel("up", 5)

; Close Open Windows
MouseClick("left",$InboxClose0, $InboxClose1, $ClickSpeed)
MouseClick("left",$TraderClose0, $TraderClose1, $ClickSpeed)
MouseClick("left",$GuildClose0, $GuildClose1, $ClickSpeed)

; Trade Tab
MouseClick("left",$TradeTab0,$TradeTab1,$ClickSpeed)

; Add Offer
MouseClick("left",$AddOffer0,$AddOffer1,$ClickSpeed)

; Deliver Select
MouseClick("left",$DeliverSelect0,$DeliverSelect1,$ClickSpeed)

;Case Statement
Select

Case $random == $Type1
	;------------------ Type 1  5coin for 400 HW -----------------------
	MouseClick("left", $AdvancedTab0, $AdvancedTab1, $ClickSpeed)

	MouseClick("left", $OneOne0+(($COIN0-1)*$Delta0), $OneOne1+(($COIN1-1)*$Delta1), $ClickSpeed)

	MouseClick("left", $Five0, $Five1, $ClickSpeed)
	;-------------------------------------------------------------------

Case $random == $Type2
	;------------------ Type 2 400 HWP for 27coin -----------------------
	MouseClick("left", $AdvancedTab0, $AdvancedTab1, $ClickSpeed)

	MouseClick("left", $OneOne0+(($HWP0-1)*$Delta0), $OneOne1+(($HWP1-1)*$Delta1), $ClickSpeed)

	MouseClick("left", $FourHundred0, $FourHundred1, $ClickSpeed)
	;-------------------------------------------------------------------

Case $random == $Type3
	;------------------ Type 3 10 coin for 400 HWP -----------------------
	MouseClick("left", $AdvancedTab0, $AdvancedTab1, $ClickSpeed)

	MouseClick("left", $OneOne0+(($COIN0-1)*$Delta0), $OneOne1+(($COIN1-1)*$Delta1), $ClickSpeed)

	MouseClick("left", $Ten0, $Ten1, $ClickSpeed)
	;-------------------------------------------------------------------

Case $random == $Type4
	;------------------ Type 4 400 HWP for 400 Horse -----------------------
	MouseClick("left", $AdvancedTab0, $AdvancedTab1, $ClickSpeed)

	MouseClick("left", $OneOne0+(($HWP0-1)*$Delta0), $OneOne1+(($HWP1-1)*$Delta1), $ClickSpeed)

	MouseClick("left", $FourHundred0, $FourHundred1, $ClickSpeed)
	;-------------------------------------------------------------------

Case $random == $Type5
	;------------------ Type 5 200 HWP for 400 Bows -----------------------
	MouseClick("left", $AdvancedTab0, $AdvancedTab1, $ClickSpeed)

	MouseClick("left", $OneOne0+(($HWP0-1)*$Delta0), $OneOne1+(($HWP1-1)*$Delta1), $ClickSpeed)

	MouseClick("left", $TwoHundred0, $TwoHundred1, $ClickSpeed)
	;-------------------------------------------------------------------


EndSelect

; Select OK Button
MouseClick("left", $OK0, $OK1, $ClickSpeed)

; Receive Select
MouseClick("left", $ReceiveSelect0, $ReceiveSelect1, $ClickSpeed)

;Case Statement
Select

Case $random == $Type1
	;------------------ Type 1  5coin for 400 HW -----------------------
	MouseClick("left", $AdvancedTab0, $AdvancedTab1, $ClickSpeed)

	MouseClick("left", $OneOne0+(($HW0-1)*$Delta0), $OneOne1+(($HW1-1)*$Delta1), $ClickSpeed)

	MouseClick("left", $FourHundred0, $FourHundred1, $ClickSpeed)
	;-------------------------------------------------------------------

Case $random == $Type2
	;------------------ Type 2 400 HWP for 27coin  -----------------------
	MouseClick("left", $AdvancedTab0, $AdvancedTab1, $ClickSpeed)

	MouseClick("left", $OneOne0+(($COIN0-1)*$Delta0), $OneOne1+(($COIN1-1)*$Delta1), $ClickSpeed)

	MouseClick("left", $TwentySeven0, $TwentySeven1, $ClickSpeed)
	;-------------------------------------------------------------------

Case $random == $Type3
	;------------------ Type 3 10 coin for 400 HWP -----------------------
	MouseClick("left", $AdvancedTab0, $AdvancedTab1, $ClickSpeed)

	MouseClick("left", $OneOne0+(($HWP0-1)*$Delta0), $OneOne1+(($HWP1-1)*$Delta1), $ClickSpeed)

	MouseClick("left", $FourHundred0, $FourHundred1, $ClickSpeed)
	;-------------------------------------------------------------------

Case $random == $Type4
	;------------------ Type 4 400 HWP for 400 Horse -----------------------
	MouseClick("left", $AdvancedTab0, $AdvancedTab1, $ClickSpeed)

	MouseClick("left", $OneOne0+(($Horse0-1)*$Delta0), $OneOne1+(($Horse1-1)*$Delta1), $ClickSpeed)

	MouseClick("left", $FourHundred0, $FourHundred1, $ClickSpeed)
	;-------------------------------------------------------------------

Case $random == $Type5
	;------------------ Type 5 200 HWP for 400 Bows -----------------------
	MouseClick("left", $IntermediateTab0, $IntermediateTab1, $ClickSpeed)

	MouseClick("left", $OneOne0+(($Bow0-1)*$Delta0), $OneOne1+(($Bow1-1)*$Delta1), $ClickSpeed)

	MouseClick("left", $FourHundred0, $FourHundred1, $ClickSpeed)
	;-------------------------------------------------------------------

EndSelect

; Select OK Button
MouseClick("left", $OK0, $OK1, $ClickSpeed)


; Send Trade
MouseClick("left", $Trade0, $Trade1, $ClickSpeed)

Sleep(50)

MouseClick("left", $Trade0, $Trade1, $ClickSpeed)


;------------------------- Return to last window ----------------------------------------------

If $Here Then
MouseMove($MousePosX, $MousePosY, $ClickSpeed)

; Activate last known Window and move mouse back to where it was
WinActivate($WindowHandle)
;WinWaitActive($WindowHandle,"")
EndIf

Sleep($SleepTime)

Beep()

WEnd

; Functions -----------------------------------------------------------------------------

Func SetType1()
	$GetRandom = False
	$random = $Type1

	MsgBox(0, "SetType1", "5 coin for 400 Hardwood", $TimeoutTime)
EndFunc

Func SetType2()
	$GetRandom = False
	$random = $Type2

	MsgBox(0, "SetType2", "400 Hardwood Planks for 27 coin", $TimeoutTime)
EndFunc

Func SetType3()
	$GetRandom = False
	$random = $Type3

	MsgBox(0, "SetType3", "11 coin for 400 Hardwood Planks", $TimeoutTime)
EndFunc

Func SetType4()
	$GetRandom = False
	$random = $Type4

	MsgBox(0, "SetType4", "400 Hardwood Planks for 400 Horses", $TimeoutTime)
EndFunc

Func SetType5()
	$GetRandom = False
	$random = $Type5

	MsgBox(0, "SetType5", "200 Hardwood Planks for 400 Bows", $TimeoutTime)
EndFunc

Func SetRandom()
	$GetRandom = True

	MsgBox(0, "SetRandom", "Random", $TimeoutTime)
EndFunc

Func NightStockup()
	If $Stockup == False Then
		MsgBox(0, "SetNightTimeStockup", "On", $TimeoutTime)
		$Stockup = True
	Else
		MsgBox(0, "SetNightTimeStockup", "Off", $TimeoutTime)
		$Stockup = False
EndIf

EndFunc

Func Here()
	$Here = True
	MsgBox(0, "Here", "HERE!", $TimeoutTime)
EndFunc

Func NotHere()
	$Here = False
	MsgBox(0, "Not Here", "BE GONE!", $TimeoutTime)
EndFunc

;-----------------------------------------------------------------------------------------------------

Func end()
   Exit
EndFunc

Func _WinWaitActivate($title,$text,$timeout=0)
	WinWait($title,$text,$timeout)
	If Not WinActive($title,$text) Then WinActivate($title,$text)
	WinWaitActive($title,$text,$timeout)
EndFunc

;End --------------------------------------------------------------------------------------------------
